package com.example.demo.config;

import javax.sql.DataSource;

import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.annotation.MapperScan;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;

import com.alibaba.druid.pool.DruidDataSource;

@Configuration
@MapperScan(basePackages = "com.example.demo.historyDao", sqlSessionTemplateRef  = "historySqlSessionTemplate")
public class HistoryDataSourceConfiguration {
	public final static org.slf4j.Logger logger = LoggerFactory.getLogger(HistoryDataSourceConfiguration.class);
	
	@Value("${spring.datasource.first-url}")
	private String url;
	
	@Value("${spring.datasource.first-username}")
	private String username;
	
	@Value("${spring.datasource.first-password}")
	private String password;
	
	@Value("${spring.datasource.historyUrl}")
	private String historyUrl;
	
	@Value("${spring.datasource.historyUsername}")
	private String historyUsername;
	
	@Value("${spring.datasource.historyPassword}")
	private String historyPassword;
	
	@Value("${spring.datasource.driver-class-name}")
	private String driverClassName;
	
	@Value("${spring.datasource.druid.initialSize}")
	private int initialSize;
	
	@Value("${spring.datasource.druid.minIdle}")
	private int minIdle;
	
	@Value("${spring.datasource.druid.maxActive}")
	private int maxActive;
	
	@Value("${spring.datasource.druid.maxWait}")
	private int maxWait;
	
	@Value("${spring.datasource.druid.timeBetweenEvictionRunsMillis}")
	private int timeBetweenEvictionRunsMillis;
	
	@Value("${spring.datasource.druid.minEvictableIdleTimeMillis}")
	private int minEvictableIdleTimeMillis;
	
	@Value("${spring.datasource.druid.validationQuery}")
	private String validationQuery;
	
	@Value("${spring.datasource.druid.testWhileIdle}")
	private boolean testWhileIdle;
	
	@Value("${spring.datasource.druid.testOnBorrow}")
	private boolean testOnBorrow;
	
	@Value("${spring.datasource.druid.testOnReturn}")
	private boolean testOnReturn;
    
    @Bean(name = "historyDataSource")     
    public DataSource historyDataSource(){  
    	logger.info("初始化历史库数据库连接池");
        DruidDataSource datasource = new DruidDataSource();  
        datasource.setUrl(historyUrl);  
        datasource.setUsername(historyUsername);  
        datasource.setPassword(historyPassword);  
        datasource.setDriverClassName(driverClassName);  
  
        //configuration  
        datasource.setInitialSize(initialSize);  
        datasource.setMinIdle(minIdle);  
        datasource.setMaxActive(maxActive);  
        datasource.setMaxWait(maxWait);  
        datasource.setTimeBetweenEvictionRunsMillis(timeBetweenEvictionRunsMillis);  
        datasource.setMinEvictableIdleTimeMillis(minEvictableIdleTimeMillis);  
        datasource.setValidationQuery(validationQuery);  
        datasource.setTestWhileIdle(testWhileIdle);  
        datasource.setTestOnBorrow(testOnReturn);  
        datasource.setTestOnReturn(testOnReturn);  
        logger.info("初始化历史库数据库连接池完成");
        return datasource;  
    } 

    @Bean(name = "historyTransactionManager")
    public DataSourceTransactionManager historyTransactionManager(@Qualifier("historyDataSource")DataSource dataSource) {
        return new DataSourceTransactionManager(dataSource);
    }
    
    
    @Bean(name = "historySqlSessionFactory")
    public SqlSessionFactory historySqlSessionFactory(@Qualifier("historyDataSource") DataSource dataSource) throws Exception {
        SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
        bean.setDataSource(dataSource);
        bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:mapper/*.xml"));
        bean.setTypeAliasesPackage("com.example.demo.model");
        return bean.getObject();
    }
    
    @Bean(name = "historySqlSessionTemplate")
    public SqlSessionTemplate historySqlSessionTemplate(@Qualifier("historySqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception {
        return new SqlSessionTemplate(sqlSessionFactory);
    }
}
